package dp;

import java.util.Arrays;

public class KnapsackProblemComplete {
    //完全背包问题
    public static void main(String[] args) {
        KnapsackProblem.Item[] items = new KnapsackProblem.Item[]{
                new KnapsackProblem.Item(1, "青铜", 2, 3),
                new KnapsackProblem.Item(2, "白银", 3, 4),
                new KnapsackProblem.Item(3, "黄金", 4, 7),
        };
        select(items, 6);
    }

    private static void select(KnapsackProblem.Item[] items, int total) {
        int[][] ints = new int[items.length][total + 1];
        //格式化第一行
        for (int k = 0; k < total + 1; k++) {
            if (k >= items[0].weight) {
                //当前物品价值+ 剩余空间最大的重量
                ints[0][k] = items[0].value + ints[0][k - items[0].weight];
            }
        }

        for (int i = 1; i < ints.length; i++) {
            KnapsackProblem.Item item = items[i];
            for (int j = 0; j < total + 1; j++) {
                if (j >= item.weight) {
                    ints[i][j] =Integer.max( item.value + ints[i][j - item.weight],ints[i - 1][j]);
                } else {
                    ints[i][j] = ints[i - 1][j];
                }
            }
        }

        print(ints);
    }

    private static void print(int[][] dp) {
        System.out.println("=====");
        for (int[] ints : dp) {
            System.out.println(Arrays.toString(ints));
        }
        System.out.println("=====");
    }
}
